<?php

/*
	function test111(){
		$objResponse = new xajaxResponse();
	    $objResponse->addAlert("szdgfsdfg sdf sdfg sdfg ");
		    
	    return $objResponse;
	}

	$xajax = new xajax();
	$xajax->debugOn();
	$xajax->registerFunction("test111");
	
	$xajax->processRequests();
	
	$xajax->printJavascript();			
	
*/
	

		

	require_once('model/User.php');
	require_once('model/Question.php');
	require_once('model/Car.php');

	final class Admin extends Controller{

		//��������, �������� �� ��������������� ������������� ������� ����� ���������� �� ��������� ����������� "/"

		
		protected function CheckAdmin($direct="/"){
			$isAdmin =CurrentUserIsAdmin();
			if(!$isAdmin){
				$this->Redirect($direct);
				$this->Headers();
				exit();
			}
		}


		function __construct($params = array() ) {
			parent::__construct($params);
		}


		public function Index(){
			$this->CheckAdmin();
			$this->headers['Content-type'] = 'text/html';
		}

		public function Questions(){
			$this->CheckAdmin();
			$this->stylesheet = 'Unansed-form';
			$this->page->MountSource(Question::Questions());
		}

		public function Question(){
			$this->CheckAdmin();
			if($this->params['subaction']=='remove'){
				$obj = new Question($this->params['qid']);
				$obj->Remove();
				$this->RedirectBack();
				return ;
			}
			if($this->params['subaction']=='save') {
					$obj = new Question($this->params['qid']);
					$obj->Content(mes(htmlspecialchars($this->request['content'])));
					$obj->Answer(mes(htmlspecialchars($this->request['answer'])));
					$obj->Save();
					$this->RedirectBack(2);
					return ;

			}

			$obj = new Question($this->params['qid']);
			$obj->Load();
			$this->page->MountSource($obj->AsSource());

			$author = new User($obj->Owner());
			$author->Load();
			$this->page->MountSource($author->AsSource('Author'));

			$this->stylesheet = 'Answer-form';
		}

		public function Faq(){
			$this->CheckAdmin();
			$this->stylesheet = 'FAQ-form';
			$this->page->MountSource(Question::FAQ());

		}

		public function MyCars(){
			if( CurrentUserIsAdmin()) {
				$this->page->MountSource(Car::CarsByDeveloper('',CAR_STATUS_BYOWNER,CAR_ACTIVE_ONVIEW));
				$this->stylesheet = 'Admin-car-list';
			}
			else {
				$this->page->MountSource(Car::OwnCars());
				$this->stylesheet = 'Car-list';
			}
			

		}

		public function CarList(){
			$this->stylesheet = 'Admin-car-list';
			if(CurrentUserIsAdmin()) {
				if($this->params['status'] == 'salon') $status = CAR_STATUS_SALON;
				else if($this->params['status'] == 'asorder') $status = CAR_STATUS_ASORDER;
				$this->page->MountSource(Car::CarsByDeveloper('',$status,CAR_ACTIVE_SHOW));
				$this->page->MountSource(Car::CarsByDeveloper('',$status,CAR_ACTIVE_ONVIEW));
				$this->page->MountSource(Car::CarsByDeveloper('',$status,CAR_ACTIVE_ARCHIVE));
			}
			else if($this->params['status'] == 'salon') $this->Redirect('/cars/salon');
			else if ($this->params['status'] == 'asorder') $this->Redirect('/cars/asorder');
		}

		public function CarInfo(){
			$this->page->MountSource(CarDeveloper::AllDevelopers());
			$this->page->MountSource(CarModel::AllModels());			
			$this->page->MountSource(Car::States());			
			
			$this->stylesheet = 'Car-edit';
			$obj = new Car();
			$obj->InfoId($this->params['infoId']);
			$obj->Load();
			if($obj->CanEdit()){
				$car_source = $obj->AsSource();

				$author = new User($obj->Owner());
				$author->Load();

				$car_source->MountSource($author->AsSource('Author'));
				$this->page->MountSource($car_source);
			}
			else throw new Exception("Acsess denied");
		}

		public function CarAction(){

			
			$this->stylesheet = 'Car-edit';
			$obj = new Car();
			$obj->InfoId($this->params['infoId']);
			$obj->Load();
			if($this->params['command'] == 'public'){
				$obj->Active(CAR_ACTIVE_SHOW);
				$obj->Save();
				$this->RedirectBack(1);
			}
			else if($this->params['command'] == 'remove'){
				$obj->Remove();
				$this->RedirectBack(1);

			}
			else if($this->params['command'] == 'archive'){
				$obj->Active(CAR_ACTIVE_ARCHIVE);
				$obj->Save();
				$this->RedirectBack(1);
			}
			else if($this->params['command'] == 'save'){
				$model = new CarModel(mes(htmlspecialchars($this->request['model'])));
				$model->Load();
				$obj->Model($model);
				$obj->Enginev(mes(htmlspecialchars($this->request['enginev'])));
				$obj->Year(mes(htmlspecialchars($this->request['year'])));
				$obj->Path(mes(htmlspecialchars($this->request['path'])));
				$obj->Price(mes(htmlspecialchars($this->request['price'])));
				$obj->Color(mes(htmlspecialchars($this->request['color'])));
				$obj->State(mes(htmlspecialchars($this->request['state'])));
				$obj->Description(mes(htmlspecialchars($this->request['description'])));
				$obj->Active(mes(htmlspecialchars($this->request['active'])));
				$obj->Status(mes(htmlspecialchars($this->request['status'])));
				$obj->Save();
				
				//small img
				$new_path = $_SERVER['DOCUMENT_ROOT'].'/media/cars/'.$obj->Id();
				if(!is_dir($new_path)) mkdir($new_path);
				$new_path.='/small.jpg';
				if($_FILES['small_img']['error'] == UPLOAD_ERR_OK){
					
					if(is_file($new_path)) unlink($new_path);
					move_uploaded_file($_FILES['small_img']['tmp_name'], $new_path);
				}
				else {
					//TODO error no image
				}				
				
				//big img
				$new_path = $_SERVER['DOCUMENT_ROOT'].'/media/cars/'.$obj->Id();
				if(!is_dir($new_path)) mkdir($new_path);
				$new_path.='/big.jpg';
				if($_FILES['big_img']['error'] == UPLOAD_ERR_OK){
					
					if(is_file($new_path)) unlink($new_path);
					move_uploaded_file($_FILES['big_img']['tmp_name'], $new_path);
				}
				else {
					$obj->Elite(CAR_ELITE_NO);
					//TODO error no image
				}					
				$obj->Save();
				
				
				$this->RedirectBack(1);
			}

		}

		public function AddCar(){
			$this->page->MountSource(CarDeveloper::AllDevelopers());
			$this->page->MountSource(CarModel::AllModels());
			$this->page->MountSource(Car::States());
			
			
			$this->stylesheet="Add-car";
			

			
			
			if($this->params['subaction'] == 'check'){
				$carmodel = new CarModel($this->request['model']);
				$carmodel->Load();
				

				
				$car = new Car();
				$car->Model($carmodel);
				$car->Enginev(mes(htmlspecialchars($this->request['enginev'])));
				$car->Year(mes(htmlspecialchars($this->request['year'])));
				$car->Path(mes(htmlspecialchars($this->request['path'])));
				$car->Price(mes(htmlspecialchars($this->request['price'])));
				$car->Color(mes(htmlspecialchars($this->request['color'])));
				$car->State(mes(htmlspecialchars($this->request['state'])));
				$car->Description(mes(CutRN(htmlspecialchars($this->request['description']))));
				$status = (int)mes(htmlspecialchars($this->request['status']));
				$car->Status($status);
				if($status == 1 || $status == 2)
					$car->Active(CAR_ACTIVE_SHOW);	
				else $car->Active(CAR_ACTIVE_ONVIEW);	
				
				$car->Save();
				
				//small img
				$new_path = $_SERVER['DOCUMENT_ROOT'].'/media/cars/'.$car->Id();
				mkdir($new_path);
				$new_path.='/small.jpg';
				if($_FILES['small_img']['error'] == UPLOAD_ERR_OK){
					if(is_file($new_path)) unlink($new_path);
					move_uploaded_file($_FILES['small_img']['tmp_name'], $new_path);
				}
				else {
					//TODO error no image
				}				
				
				//big img
				$new_path = $_SERVER['DOCUMENT_ROOT'].'/media/cars/'.$car->Id();
				$new_path.='/big.jpg';
				if($_FILES['big_img']['error'] == UPLOAD_ERR_OK){
					if(is_file($new_path)) unlink($new_path);
					move_uploaded_file($_FILES['big_img']['tmp_name'], $new_path);
					$car->Elite(CAR_ELITE_YES);
					
				}
				else {
					$car->Elite(CAR_ELITE_NO);
					//TODO error no image
				}							
				$car->Save();
				
				
				$this->RedirectBack(2);
				return;
			}

		}

	}
